package com.drinks.yiniang.adapter.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.drinks.yiniang.R;

/**
 * @author han
 * @date 2019/11/4
 */
public class NumPadAdapter extends BaseAdapter {

    private static final String NUMBERS = "123456789C0#";
    private Context mContext;
    private Drawable drawable;

    public NumPadAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;

        if (convertView == null) {
            mHolder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_numpad, null);

            mHolder.ll_item_keyboard = convertView.findViewById(R.id.ll_item_keyboard);
            mHolder.tv_item_num_keyboard = convertView.findViewById(R.id.tv_item_num_keyboard);

            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }

        String curNumber = (String) getItem(position);

        if ("C".equals(curNumber)) {
            setDrawable(mHolder.tv_item_num_keyboard, false);
            mHolder.tv_item_num_keyboard.setText(curNumber);
            mHolder.ll_item_keyboard.setBackgroundColor(Color.parseColor("#d1d5db"));
        } else if ("#".equals(curNumber)) {
            setDrawable(mHolder.tv_item_num_keyboard, true);
            mHolder.tv_item_num_keyboard.setText("");
            mHolder.ll_item_keyboard.setBackgroundColor(Color.parseColor("#d1d5db"));
        } else {
            mHolder.tv_item_num_keyboard.setText(curNumber);
            setDrawable(mHolder.tv_item_num_keyboard, false);
            mHolder.ll_item_keyboard.setBackgroundResource(R.drawable.selector_num_pad);
        }

        return convertView;
    }

    private void setDrawable(TextView tv, boolean hasDraw) {
        if (hasDraw) {
            if (drawable == null) {
                drawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_pwd_delete);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            }

            tv.setCompoundDrawables(drawable, null, null, null);
        } else {
            tv.setCompoundDrawables(null, null, null, null);
        }
    }

    @Override
    public int getCount() {
        return NUMBERS.length();
    }

    @Override
    public Object getItem(int position) {
        return String.valueOf(NUMBERS.charAt(position));
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder {
        FrameLayout ll_item_keyboard;
        TextView tv_item_num_keyboard;
    }
}